<?php
/**
 *  * 系统-受国家计算机软件著作权保护 - !
 * =========================================================
 * Copy right 2018-2025 成都海之心科技有限公司, 保留所有权利。
 * ----------------------------------------------
 * 官方网址: http://www.ohyu.cn
 * 这不是一个自由软件！在未得到官方有效许可的前提下禁止对程序代码进行修改和使用。
 * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
 * =========================================================
 * User: ohyueo
 * Date: 2022/6/5
 * Time: 13:51
 */
declare (strict_types = 1);

namespace app\listener\api;
use think\facade\Db;

class SendYuyueMessageListener
{
    /**
     * 事件监听处理
     *
     * @return mixed
     */
    public function handle($event)
    {
        $this->sendMessage($event['data'],$event['type']);
    }

    /**
     * 给用户发送消息
     *
     * @param $user
     * @param $title
     * @param $text
     */
    public function sendMessage($data,$type)
    {
        if($type==1){ //开班通知
            $uid=$data['uid'];
            $id=$data['id'];
            $name=$data['name'];
            $title=$data['title'];
            $riqi=$data['riqi'];
            $tid=config('-remindsite.remind_gzh_rubantz');
            if($tid) {
                $openid = Db::name('tender_user_list')->where('id', $uid)->value('openid');
                if ($openid) {
                    $wechat = new \app\common\WeChat();
                    $tmparr = array(
                        "first" => [
                            "value" => '您好，您的培训'.$title.'将于'.$riqi.'开班！'
                        ],
                        "keyword1" => [ //学员姓名
                            "value" => $name
                        ],
                        "keyword2" => [  //加入课堂
                            "value" => $title
                        ],
                        "keyword3" => [  //加入时间
                            "value" => date('Y年m月d日')
                        ],
                        "remark" => [
                            "value" => '请定期前往。'
                        ]
                    );
                    $url = config('-appsite.app_domainname') . "#/pages/order/myorder_info?id=" . $id;
                    $wechat->sendh5Message($openid, $tid, $tmparr, $url);
                }
            }
        }else if($type==2){ //报名提醒
            $id=$data['id'];
            $title=$data['title'];
            $tid=config('-remindsite.remind_gzh_baomingtx');
            if($tid) {
                $wechat = new \app\common\WeChat();
                $tmparr = array(
                    "first" => [
                        "value" => '您好，您的培训'.$title.'已经开始报名了！'
                    ],
                    "keyword1" => [ //姓名
                        "value" => '泰信培训'
                    ],
                    "keyword2" => [  //电话
                        "value" => '0510-86871135'
                    ],
                    "keyword3" => [  //项目
                        "value" => $title
                    ],
                    "keyword4" => [  //时间
                        "value" => date('Y年m月d日')
                    ],
                    "remark" => [
                        "value" => '请前往查看。'
                    ]
                );
                $url = config('-appsite.app_domainname');// . "#/pages/order/myorder_info?id=" . $id;

                //查询所有人  给所有人通知
                $usarr=Db::name('tender_user_list')->where('status', 1)
                    ->where('openid','not null')->field(['openid'])->select()->toArray();
                if($usarr){
                    for($i=0;$i<count($usarr);$i++){
                        $openid = $usarr[$i]['openid'];
                        if ($openid) {
                            $wechat->sendh5Message($openid, $tid, $tmparr, $url);
                        }
                    }
                }

            }
        }else if($type==3){ //退款通知
            $id=$data['id'];
            $money=$data['money'];
            $uid=$data['uid'];
            $msg=$data['msg'];
            $tit=$data['tit'];
            //通知模板消息  公众号
            $succ=config('-remindsite.remind_gzh_tmptuikuan');
            if($succ){
                $openid=Db::name('tender_user_list')->where('id',$uid)->value('openid');
                if($openid){
                    $wechat = new \app\common\WeChat();
                    $tmparr=array(
                        "first" => [
                            "value"=> $tit
                        ],
                        "keyword1" => [
                            "value"=> $id
                        ],
                        "keyword2" => [
                            "value"=> $money
                        ],
                        "remark" => [
                            "value"=> $msg
                        ]
                    );
                    $url=config('-appsite.app_domainname')."#/pages/order/myorder_info?id=".$id;
                    $wechat->sendh5Message($openid,$succ,$tmparr,$url);
                }
            }
        }else if($type==4) { //开票成功通知
            $id=$data['id'];
            $title=$data['title'];
            $uid=$data['uid'];
            $money=$data['money'];
            $tid=config('-remindsite.remind_gzh_sucopenpiao');
            if($tid){
                $openid=Db::name('tender_user_list')->where('id',$uid)->value('openid');
                if($openid){
                    $wechat = new \app\common\WeChat();
                    $tmparr=array(
                        "first" => [
                            "value"=> '您的发票已经开出，请注意查收。网上报名的一律电子发票，无纸质发票。'
                        ],
                        "keyword1" => [ //商户名称
                            "value"=> '江阴市泰信安全技术服务有限公司'
                        ],
                        "keyword2" => [ //开票金额
                            "value"=> $money
                        ],
                        "keyword3" => [ //发票抬头
                            "value"=> $title
                        ],
                        "keyword4" => [ //开票时间
                            "value"=> gettime()
                        ],
                        "remark" => [
                            "value"=> '如留邮箱的，发票会发送到您的邮箱，如没留邮箱，会通过短信发送给您。咨询电话：0510-86871135'
                        ]
                    );
                    $url='';//config('-appsite.app_domainname')."#/pages/order/myorder_info?id=".$id;
                    $wechat->sendh5Message($openid,$tid,$tmparr,$url);
                }
                //Db::name('yuyue_order')->where('id',$id)->update(['tz'=>1]);
                //YuyueListmsgHandler::add('到期提醒-订单即将开始',$id,2,'到期提醒');
            }
        }else if($type==5){ //新订单
            $title=$data['title'];
            $frtit=$data['frtit'];
            //通知模板消息  公众号
            $succ=config('-remindsite.remind_gzh_neworder');
            if($succ){
                //查询满足条件的管理员
                $res=Db::name('tender_employees')->where('status',1)
                    ->select()->toArray();
                if($res){
                    for($i=0;$i<count($res);$i++){
                        $uid=$res[$i]['uid'];
                        $openid=Db::name('tender_user_list')->where('id',$uid)->value('openid');
                        if($openid){
                            $wechat = new \app\common\WeChat();
                            $tmparr=array(
                                "first" => [
                                    "value"=> $frtit?$frtit:'有用户提交了新的投标订单'
                                ],
                                "keyword1" => [
                                    "value"=> $title
                                ],
                                "keyword2" => [
                                    "value"=> gettime()
                                ],
                                "remark" => [
                                    "value"=> '请及时查看'
                                ]
                            );
                            $url=config('-appsite.app_domainname')."#/pages/user/index";
                            $wechat->sendh5Message($openid,$succ,$tmparr,$url);
                        }
                    }
                }
            }
        }else if($type==6){  //审核通过通知
            $uid=$data['uid'];
            $tid=config('-remindsite.remind_gzh_shensus');
            if($tid) {
                $openid = Db::name('tender_user_list')->where('id', $uid)->value('openid');
                if ($openid) {
                    $wechat = new \app\common\WeChat();
                    $tmparr = array(
                        "first" => [
                            "value" => '您好，您提交的认证已经审核通过'
                        ],
                        "keyword1" => [ //审核类型
                            "value" => '企业认证'
                        ],
                        "keyword2" => [  //审核结果
                            "value" => '审核通过'
                        ],
                        "keyword3" => [  //审核时间
                            "value" => date('Y年m月d日')
                        ],
                        "remark" => [
                            "value" => '请点击该处进行查看！'
                        ]
                    );
                    $url = config('-appsite.app_domainname') . "#/pages/user/index";
                    $wechat->sendh5Message($openid, $tid, $tmparr, $url);
                }
            }
        }else if($type==7){  //审核拒绝通知
            $uid=$data['uid'];
            $msg=$data['msg'];
            $tid=config('-remindsite.remind_gzh_shenerr');
            if($tid) {
                $openid = Db::name('tender_user_list')->where('id', $uid)->value('openid');
                if ($openid) {
                    $wechat = new \app\common\WeChat();
                    $tmparr = array(
                        "first" => [
                            "value" => '您好，您提交的认证审核失败。'
                        ],
                        "keyword1" => [ //审核类型
                            "value" => '企业认证'
                        ],
                        "keyword2" => [  //拒绝原因
                            "value" => $msg
                        ],
                        "keyword3" => [  //审核时间
                            "value" => date('Y年m月d日')
                        ],
                        "remark" => [
                            "value" => '您好，请点击该处，重新完善后提交。'
                        ]
                    );
                    $url = config('-appsite.app_domainname') . "#/pages/user/index";
                    $wechat->sendh5Message($openid, $tid, $tmparr, $url);
                }
            }
        }
    }
}